package com.wyh.domain.vo.groupOne.utils.ThreemeterReadingExcelUtils;

import com.wyh.domain.vo.groupOne.threemeterTree.threemeterReading.ThreemeterReadingExcelVO;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

public class ThreemeterReadingExcelExporter {

    public static String exportToExcel(List<ThreemeterReadingExcelVO> excelVOList, String name) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("三表抄表明细");

        // 创建标题行
        String[] headers = {"序号", "三表性质", "三表编号", "三表名称", "所属资源", "抄表周期", "上期抄表时间", "本期读数", "本期用量", "状态"};
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        int rowNum = 1;
        for (ThreemeterReadingExcelVO vo : excelVOList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(vo.getId());
            row.createCell(1).setCellValue(vo.getNature());
            row.createCell(2).setCellValue(vo.getNumber());
            row.createCell(3).setCellValue(vo.getName());
            row.createCell(4).setCellValue(vo.getInfoname());
            row.createCell(5).setCellValue(vo.getPeriod());
            // 检查 vo.getPreviousperiodtime() 是否有值
            if (vo.getPreviousperiodtime() != null) {
                row.createCell(6).setCellValue(vo.getPreviousperiodtime().format(formatter));
            } else {
                row.createCell(6).setCellValue("无上期抄表时间");
            }
            row.createCell(7).setCellValue(vo.getReading());
            row.createCell(8).setCellValue(vo.getDosage());
            row.createCell(9).setCellValue(vo.getIscharging());
        }

        // 生成随机文件名并指定路径（保存到桌面）
        String userHome = System.getProperty("user.home");
        String desktopPath = userHome + "/Desktop/";
        String sanitizedFileName = name.replaceAll("[\\\\/:*?\"<>|]", "_");
        String fileName = desktopPath + sanitizedFileName + "_三表抄表明细_" + UUID.randomUUID().toString() + ".xlsx";

        // 写入 Excel 文件
        try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
            workbook.write(fileOut);
            File file = new File(fileName);
            if (file.exists() && file.length() > 0) {
                return "Excel文件已成功生成: " + fileName;
            } else {
                return "Excel文件生成失败";
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "Excel文件生成失败";
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
